package com.servicesystem.servicesystem.controller;

import com.servicesystem.servicesystem.entity.AliTtGoods;
import com.servicesystem.servicesystem.service.AliTtGoodsService;
import com.servicesystem.servicesystem.untis.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/bind")
public class AliTtGoodsController {
    @Autowired
    private AliTtGoodsService aliTtGoodsService;

    @PostMapping("add/goods/bind")
    public R saveAliTtGoods(@RequestBody List<AliTtGoods> aliTtGoodsList) {
        List<String> skuIds = new ArrayList<>();
        for (AliTtGoods aliTtGoods : aliTtGoodsList) {
            StringBuilder uid = new StringBuilder();
            Random rd = new SecureRandom();
            for (int i = 0; i < 16; i++) {
                uid.append(rd.nextInt(10));
            }
            skuIds.add(uid.toString());
            aliTtGoods.setCspecId(uid.toString());
        }
        aliTtGoodsService.saveBatch(aliTtGoodsList);
        return R.success().data(skuIds);
    }

    @GetMapping("get/goods-bind/{skuId}/{offerId}")
    public R getAliTtGoods(@PathVariable(value = "skuId") String skuId,
                           @PathVariable(value = "offerId") String offerId) {
        AliTtGoods aliTtGoods = aliTtGoodsService.getAliTtGoods(skuId, offerId);
        if (aliTtGoods == null) {
            return R.fail("失败").data(new AliTtGoods().setCspecId("1"));
        }
        return R.success().data(aliTtGoods);
    }
}
